﻿using DiverseStudents.UI;
using HarmonyLib;
using System;
using UnityEngine;
using View.Main;

namespace DiverseStudents.Mods
{
    internal class ModsView : ModsUI
    {

        [HarmonyPostfix, HarmonyPatch(typeof(EntryView), "InitUI")]
        public static void NewInitUI(EntryView __instance)
        {
            var group = (RectTransform)Traverse.Create(__instance).Field("group_top").GetValue();
            try
            {
                if (group != null)
                {
                    var btn = UICreate.Button(OpenView, ButtonsContent.initName);
                    btn.transform.sizeDelta = new Vector2(90, 40);
                    btn.transform.localPosition = new Vector3(1200, 0, 0);
                    btn.transform.SetParent(group, false);
                    btn.gameObject.SetActive(true);
                }
            }
            catch (Exception e)
            {
                DLog.Error(e);
            }
        }


        private static void OpenView()
        {
            if (normal == null)
            {
                normal = GameObject.Find("UI_Normal");
                mask = UICreate.Mask();
            }
            if (normal != null)
            {
                //遮罩
                mask?.transform.SetParent(normal.transform, false);

                //背景
                baseRect = UICreate.SettingRect();
                var ts = normal.GetComponent<RectTransform>();
                if (ts != null)
                    baseRect.sizeDelta = new Vector2(ts.rect.width * 9 / 10, ts.rect.height * 9 / 10);
                baseRect.SetSiblingIndex(2);
                baseRect.SetParent(normal.transform, false);

                //关闭按钮
                btnClose = UICreate.CloseButton(CloseView);

                var ct = btnClose.transform.GetComponent<RectTransform>();
                btnClose.transform.SetSiblingIndex(4);
                if (ct != null)
                    ct.anchoredPosition += new Vector2(baseRect.rect.width / 2, baseRect.rect.height / 2);
                btnClose.transform.SetParent(baseRect, false);

                //详情背景
                scrollRight.UpdateRect(baseRect);

                topButtons.UpdateRect(baseRect);

                modsList.UpdateRect(baseRect);

                modsList.AddItem();

            }
            else
            {
                DLog.Warring("Not find UI_Normal");
            }
        }

        private static void CloseView()
        {
            if (normal != null)
            {

                if (mask != null)
                {
                    var tf = normal.transform.Find("Mod_Mask");
                    tf?.SetParent(null, false);
                }
                GameObject.Destroy(baseRect.gameObject);
                scrollRight.Destroy();
                modsList.Destroy();
                topButtons.Destroy();
            }
        }

        void OnEnable()
        {
            Application.focusChanged += OnFocusChanged;
        }

        void OnDisable()
        {
            Application.focusChanged -= OnFocusChanged;
        }


        void OnFocusChanged(bool hasFocus)
        {
            if (hasFocus)
            {
                /* 游戏窗口再次获得焦点,刷新当前列表显示的mod
                拖动显示到mod项时刷新当前项
                如果此时列表被关闭将剩余项进行刷新，然后读取
                 */
               
            }
        }

    }
}
